;#
;# mpy_32_16_asm.S
;#
;# Multiply a 16 bit integer by a 32 bit. The result is divided by 2**15
;# 
;# Oded Rosen, 2011   
;#

	.text
	.globl Mpy_32_16
	
	.arm
	
;#************************************************************************
;#
;#  FUNCTION: Mpy_32_16 
;#
;#  Multiply a 16 bit integer by a 32 bit. The result is divided by 2**15
;#
;#
;#   L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1
;#
;# Arguments:
;#
;#  hi          hi part of 32 bit number.
;#  lo          lo part of 32 bit number.
;#  n           16 bit number.
 ;#************************************************************************/

	;# inputs / outputs:
	hi			.req r0		;# (i)  : hi part of 32 bit number
    lo			.req r1		;# (i)  : lo part of 32 bit number
    n			.req r2		;# (o)  : 16 bit number.

Mpy_32_16:

		;# Stack current values:
		;# PUSH		{r4}		;# stack
		
		smulbb		hi,hi,n
		QADD		hi,hi,hi
		
		MUL 		lo,lo,n		
		ASR 		lo,lo,#15		
		SSAT 		lo,#16,lo
		
		QDADD		hi,hi,lo
		
		;# Unstack values and return:
		;# POP			{r4}			;# unstack
		BX			lr				;# return
